<!--
 * @Description: file content
 * @Author: oooh
 * @Date: 2024-04-26 11:16:35
 * @LastEditors: oooh
 * @LastEditTime: 2024-08-07 16:22:22
 * @FilePath: \Civil_Servant_Performance_PC\src\views\performance-inventory\dept\jjb-evaluation-comment.vue
-->
<template>
  <div class="component-container">
    <div class="flex-table">
      <el-table :data="workList" border height="100%" v-loading="loading">
        <el-table-column label="序号" type="index" align="center">
          <template slot-scope="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="deptName" label="站办所" width="180" align="center"> </el-table-column>
        <el-table-column v-for="(item, index) in columns" :key="item.value" :label="item.label" prop="total" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.normList[index].total }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="otherScore" label="其他得分" width="150" align="center"> </el-table-column>
        <el-table-column prop="materialUrls" label="佐证材料" min-width="180" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.materialUrls">
              <el-tooltip
                v-for="(url, index) in scope.row.materialUrls.split(',')"
                :key="index"
                effect="dark"
                :content="formatFileUrl(url)['name']"
                placement="top-start"
              >
                <el-button type="text" @click="handlePreviewFile(url)" class="file-preview-btn">
                  {{ formatFileUrl(url)['name'] }}
                </el-button>
              </el-tooltip>
            </div>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column prop="add" label="加分" width="70" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.otherScore > 0 ? scope.row.otherScore : 0 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="reduce" label="扣分" width="70" align="center">
          <template slot-scope="scope">
            <span>{{ rowScoreComputeReduce(scope.row) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="总得分" align="center">
          <template slot-scope="scope">
            <span>{{ rowScoreCompute(scope.row) }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { list } from '@/api/work-approval/evaluation-comment/discipline-inspection'
import { getDicts } from '@/api/system/dict/data.js'
import FilePreview from '@/components/FilePreview'
import { mixins } from '@/mixins'
export default {
  name: 'performance-inventory_dept_jjb-evaluation-commentn',
  mixins: [mixins],
  dicts: [],
  props: {
    queryForm: {
      type: Object,
      default: () => {}
    }
  },
  components: { FilePreview },
  data() {
    return {
      tableTotal: 0,
      filePreviewVisible: false,
      previewFile: {},
      columns: [],
      workMaxScore: 0, // 纪检办评价评议分数
      loading: false,
      workList: [],
      queryParams: {}
    }
  },
  computed: {},
  watch: {
    queryForm: {
      deep: true,
      immediate: true,
      handler(query) {
        this.queryParams = { ...this.queryParams, ...query }
        // this.getWorkScore()
        this.getList()
      }
    }
  },
  created() {},
  methods: {
    // 预览文件
    handlePreviewFile(url) {
      let fileItem = this.formatFileUrl(url)
      this.previewFile = { url, ...fileItem }
      this.filePreviewVisible = true
    },
    // 扣分计算
    rowScoreComputeReduce(raw) {
      let totalScore = raw.normList.reduce((prev, curr) => {
        let score = curr.total
        if (!curr.total) score = 0
        return prev + score
      }, 0)
      totalScore *= -0.1
      if (raw.otherScore < 0) {
        totalScore = totalScore + raw.otherScore
      }
      return totalScore
    },
    // 表格行扣分计算
    rowScoreCompute(raw) {
      let totalScore = raw.normList.reduce((prev, curr) => {
        let score = curr.total
        if (!curr.total) score = 0
        return prev + score
      }, 0)
      totalScore *= -0.1
      totalScore = totalScore + raw.otherScore || 0
      return totalScore

      // let totalScore = raw.normList.reduce((prev, curr) => {
      //   let score = curr.total
      //   if (!curr.total) score = 0
      //   return prev + score
      // }, 0)
      // totalScore *= -0.1
      // totalScore = totalScore + raw.otherScore || 0
      // if (totalScore >= 0) return totalScore
      // if (Math.abs(totalScore) < this.workMaxScore) {
      //   if (Number.isInteger(totalScore)) {
      //     return totalScore
      //   } else {
      //     return totalScore.toFixed(1)
      //   }
      // }
      // return this.workMaxScore * -1
    },

    getList() {
      this.loading = true
      list(this.queryParams)
        .then(res => {
          this.loading = false
          this.tableTotal = res.total
          this.workList = res.data
          this.columns = res.data[0]['normList'].map(item => {
            return { label: item.normLabel, value: item.normDict }
          })
        })
        .catch(err => (this.loading = false))
    },
    // 刷新
    handleRefresh() {
      this.getList()
    },

    // 获取工作分数限制
    getWorkScore() {
      getDicts('biz_dept_norm_config').then(res => {
        let { data } = res
        this.workMaxScore = data.find(item => item.dictValue === 'JJB_PJPY')?.remark
      })
    }
  }
}
</script>

<style lang="scss">
.el-input-number--medium {
  width: auto;
}
.file-preview-btn {
  width: 100%;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  margin-left: 0;
  padding: 0;
}
</style>
